﻿#region Includes

using System;
using System.Reflection;
using Object=ArtheaEngine.Model.Object;

#endregion

namespace ArtheaEngine.Model
{
    public abstract partial class Ability : IEntity, ICloneable
    {
        public void DisplayOffMsg(object from)
        {
            string msg;

            if (from == null)
                return;

            if (from is Character)
            {
                var ch = from as Character;
                msg = GetAbilityMsg("MsgOffSelf");
                ch.WriteLine(msg);
                msg = GetAbilityMsg("MsgOffChar");
                ch.Act(ActType.Room, null, null, msg);
            }
            else if (from is Object)
            {
                var item = from as Object;
                if (item.CarriedBy != null || item.Room != null)
                {
                    msg = GetAbilityMsg("MsgOffObj");
                    if (item.CarriedBy != null)
                    {
                        item.CarriedBy.Act(ActType.Char, null, null, msg);
                    }
                    else if (item.Room != null)
                    {
                        foreach (var ch in item.Room.Characters)
                        {
                            ch.Act(ActType.Char, null, null, msg);
                        }
                    }
                }
            }
            else if (from is Room)
            {
                var room = from as Room;
                msg = GetAbilityMsg("MsgOffRoom");

                foreach (var ch in room.Characters)
                {
                    ch.WriteLine(msg);
                }
            }
        }

        protected virtual string GetAbilityMsg(string name)
        {
            var fi = GetType().GetField(name,
                                        BindingFlags.Static | BindingFlags.NonPublic |
                                        BindingFlags.Public);
            return fi.GetValue(null) as string;
        }

        #region Implementation of ICloneable

        /// <summary>
        ///                     Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>
        ///                     A new object that is a copy of this instance.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        public object Clone()
        {
            var newObject = Activator.CreateInstance(GetType());

            ArtheaHelper.CloneObj(newObject, this);

            return newObject;
        }

        #endregion

    }
}